/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ads.presentation;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author MFA
 */
public class BookDeliveryView extends javax.swing.JFrame {
    ArrayList<String[]> targetList;
    String[] selectedReceiver;

    private ClientControllerInterface controller;
    /**
     * Creates new form BookDeliveryView
     */
    public BookDeliveryView(ClientControllerInterface c) {
        initComponents();
        controller = c;
        selectedReceiver = new String[2];
        targetList=new ArrayList<>();
        resultTable.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    JTable target = (JTable)e.getSource();
                    int rowIndex = target.getSelectedRow();
                    int columns = resultTable.getColumnCount();
                    for(int col = 0; col < columns; col++)  
                    {
                        Object o = resultTable.getValueAt(rowIndex, col);  
                        selectedReceiver[col]=o.toString();
                    }
                    //Check whether this is already added or not
                    //System.out.println("__"+targetList.size());
                    if(!checkIsAlreadyInTargetList(selectedReceiver))
                    {
                        DefaultTableModel model_ = (DefaultTableModel) targetTable.getModel();
                        model_.addRow(new Object[]{selectedReceiver[0], selectedReceiver[1]});
                        String[] newSelectedReceiver = new String[2];
                        newSelectedReceiver[0]=new String(selectedReceiver[0]);
                        newSelectedReceiver[1]=new String(selectedReceiver[1]);
                        targetList.add(newSelectedReceiver);
                        //System.out.println("rowIndex:"+rowIndex+"  "+s);
                    }
                    else{
                        outputText.append("The receiver with name: "+selectedReceiver[0]+" and office: "+selectedReceiver[1]+" is already in the target list \n");
                    }
                }
            }
            
        });
    }
    private boolean checkIsAlreadyInTargetList(String[] selectedReceiver) {
        String[] temp;
        for(int i=0; i<targetList.size(); i++){
            temp=targetList.get(i);
            if(temp[0].equals(selectedReceiver[0]) && temp[1].equals(selectedReceiver[1])) {
                return true;
            }
        }
        return false;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jInternalFrame1 = new javax.swing.JInternalFrame();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane7 = new javax.swing.JScrollPane();
        resultTable = new javax.swing.JTable(){
            public boolean isCellEditable(int rowIndex, int colIndex) {
                return false;   //Disallow the editing of any cell
            }
        };
        confirmAllButton = new javax.swing.JButton();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane8 = new javax.swing.JScrollPane();
        targetTable = new javax.swing.JTable(){
            public boolean isCellEditable(int rowIndex, int colIndex) {
                return false;   //Disallow the editing of any cell
            }
        };
        clearTargetList = new javax.swing.JButton();
        nameField = new javax.swing.JTextField();
        officeField = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        outputText = new javax.swing.JTextArea();

        jInternalFrame1.setVisible(true);

        javax.swing.GroupLayout jInternalFrame1Layout = new javax.swing.GroupLayout(jInternalFrame1.getContentPane());
        jInternalFrame1.getContentPane().setLayout(jInternalFrame1Layout);
        jInternalFrame1Layout.setHorizontalGroup(
            jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jInternalFrame1Layout.setVerticalGroup(
            jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                frameFocusGained(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jLabel1.setText("name");

        jButton1.setText("Search");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
        jLabel4.setText("Search");

        jLabel5.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
        jLabel5.setText("Target List:");

        jLabel2.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jLabel2.setText("office");

        resultTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Receiver", "Address"
            }
        ));
        jScrollPane7.setViewportView(resultTable);

        confirmAllButton.setText("Confirm all");
        confirmAllButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                confirmAllButtonActionPerformed(evt);
            }
        });

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "ASAP", "In 5 minutes" }));
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jLabel3.setText("When to pick up?");

        targetTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Receiver", "Address"
            }
        ));
        jScrollPane8.setViewportView(targetTable);

        clearTargetList.setLabel("Clear Target List");
        clearTargetList.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearTargetListActionPerformed(evt);
            }
        });

        nameField.setNextFocusableComponent(officeField);
        nameField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nameFieldActionPerformed(evt);
            }
        });

        officeField.setNextFocusableComponent(nameField);

        outputText.setColumns(20);
        outputText.setRows(5);
        jScrollPane1.setViewportView(outputText);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane8, javax.swing.GroupLayout.DEFAULT_SIZE, 537, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(clearTargetList)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(confirmAllButton, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jButton1)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel1)
                                            .addComponent(jLabel2))
                                        .addGap(18, 18, 18)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(nameField, javax.swing.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE)
                                            .addComponent(officeField))))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane7, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane7, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(10, 10, 10)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(nameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(officeField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1)
                        .addGap(0, 45, Short.MAX_VALUE)))
                .addGap(18, 18, 18)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane8, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(confirmAllButton)
                    .addComponent(clearTargetList)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // Obtain the result for the search
        List<String[]> data;
        data = controller.searchUser_NameOffice(nameField.getText(), officeField.getText());

        // Obtain the model for the table
        DefaultTableModel model = (DefaultTableModel) resultTable.getModel();

        // Remove all rows
        int rows = model.getRowCount(); 
        for(int i = rows - 1; i >=0; i--) {
            model.removeRow(i); 
        }

        // Add all the rows from the result
        for(String[] row : data) {
            model.addRow(new Object[]{row[0], row[1]});
        }
        //jTable2.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        //jTable2.getSelectionModel().addListSelectionListener(new TableSelectionHandler());
    }//GEN-LAST:event_jButton1ActionPerformed
    /*
    class TableSelectionHandler implements ListSelectionListener{
        @Override
        public void valueChanged(ListSelectionEvent e) {
            if(e.getValueIsAdjusting()==false)
            {
                ListSelectionModel lsm = (ListSelectionModel)e.getSource();
                ++count;
                if(count==1)
                {
                    int firstIndex = e.getFirstIndex();
                    int lastIndex = e.getLastIndex();
                    int rowIndex = lsm.getLeadSelectionIndex();
                    int columns = jTable2.getColumnCount();
                    String s="";
                    for(int col = 0; col < columns; col++)  
                     {  
                         Object o = jTable2.getValueAt(rowIndex, col);  
                         s += o.toString();  
                         if(col < columns - 1)  
                             s += ",";  
                     }
                    System.out.println("rowIndex:"+rowIndex+"  "+s);
                }
                else if(count==3) count=0;
                
               }
            }
        }
    */
    private void confirmAllButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_confirmAllButtonActionPerformed
        if(!targetList.isEmpty()){
            controller.bookDelivery(
                    jComboBox1.getSelectedItem().toString(),
                    targetList,
                    this);
            DefaultTableModel model_3 = (DefaultTableModel) targetTable.getModel();
            while (model_3.getRowCount()>0){
                model_3.removeRow(0);
            }
            targetList.clear();
            outputText.append("All the deliveries in the target list are confirmed.\n");
        }
        else{
            JOptionPane.showMessageDialog(this,
                "There are no deliveries chosen submit",
                "Fail",
                JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_confirmAllButtonActionPerformed

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBox1ActionPerformed

    private void clearTargetListActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearTargetListActionPerformed
        // TODO add your handling code here: 
        DefaultTableModel model = (DefaultTableModel) targetTable.getModel();
        while (model.getRowCount()>0){
            model.removeRow(0);
        }
        targetList.clear();
        outputText.append("Target List is cleared !\n");
    }//GEN-LAST:event_clearTargetListActionPerformed

    private void nameFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nameFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_nameFieldActionPerformed

    private void frameFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_frameFocusGained
        this.nameField.requestFocus();
    }//GEN-LAST:event_frameFocusGained

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton clearTargetList;
    private javax.swing.JButton confirmAllButton;
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JInternalFrame jInternalFrame1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane7;
    private javax.swing.JScrollPane jScrollPane8;
    private javax.swing.JTextField nameField;
    private javax.swing.JTextField officeField;
    private javax.swing.JTextArea outputText;
    private javax.swing.JTable resultTable;
    private javax.swing.JTable targetTable;
    // End of variables declaration//GEN-END:variables
}

